/**
 * 帮助中心 
 */ 
<template>
  <view class="un-account-list">
    <view class="un-account-list-conten">
      <view>金额：</view>
      <view>{{ unTotalPrice }}</view>
    </view>
    <view class="un-account-list-btns">
      <button class="pay-btns" :loading="loading" @click="wxpay">去付款</button>
    </view>
  </view>
</template>

<script>
import { getLocal,setLocal } from '@/utils/uni'
export default {
  data() {
    return {
      unTotalPrice: 0,
      user: {},
      loading: false
    };
  },
  onLoad() {
    this.user = getLocal('user')
    if(this.user.openId){
      this.pageInit()
    }
  },
  onShow() {
  },
  methods: {
    pageInit(){
      this.$http.get('wechat/getWechatUserBalance', {openId : this.user.openId}, res => {
        if(res.code === 'success'){
          this.unTotalPrice = res.data
        }
      })
    },
    wxpay(){
      if(this.unTotalPrice >= 0 || this.loading){
        this.$uti.alert('不需要支付');
        return
      }
      this.loading = true
      this.$http.get('wechat/createBalanceOrder', {openId : this.user.openId}, res => {
        if(res.code === 'success'){
          let params = {
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.packageX,
            signType: res.data.signType,
            paySign: res.data.paySign
          }
          // 微信支付
          uni.requestPayment({
            provider: 'wxpay',
            orderInfo: res.data.tradeNo,
            ...params,
            success: function (res) {
              console.log('success:' + JSON.stringify(res));
              if(res.errMsg === 'requestPayment:ok'){
                _this.updateWechatUserBalance({status:1,tradeNo:res.data.tradeNo,openId:this.user.openId})
              }
            },
            fail:(fail)=>{
              this.loading = false
              _this.updateWechatUserBalance({status:2,tradeNo:res.data.tradeNo,openId:this.user.openId})
            },
          })
        }
        this.loading = false
      })
    },
    updateWechatUserBalance(params){
      this.$http.post('wechat/updateWechatUserBalance', params, res => {
        if(res.code === 'success'){
          this.$uti.alert('支付成功');
        }
        this.loading = false
      })
    }
  },
};
</script>

<style lang="scss" scoped>
.un-account-list{
  padding: 24rpx;
  .un-account-list-conten{
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 48rpx;
  }
  .un-account-list-btns{
    display: flex;
    justify-content: center;
    align-items: center;
    .pay-btns{
      color:#ffffff;
			background-color:#e69b3f;
			border:2rpx solid #e69b3f;
			width:240rpx;
			border-radius: 45rpx
    }
  }
}
</style>
